//
//  MineTopUpViewController.m
//  Srb_ios
//
//  Created by Ji on 2018/9/18.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "vgxfqhJLCFMineTopUpViewControllerBVsVle.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"
#import "XlpRCUJLCMineTopUpRecordControllerejjHxQ.h"
#import "JLCFBaseWebViewController.h"
#import "YgMWMCJLCFFuyouInfoModelNQcnsr.h"
#import "kNyTXqJLCFPayViewControllerqCkJSm.h"

@interface MineTopUpViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *rcTBzdamountTextFieldnUbSfR;

@property (nonatomic, strong) UILabel *LufDjKnameLabelRjatWG;
@property (nonatomic, strong) UIButton *msgBtn;

@property (nonatomic, strong) NSTimer *qdZZsdcountTimereVpqYS;
@property (nonatomic, assign) int count;

@end

static int const showtime = 60;

@implementation MineTopUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addNavigationBar];
    self.navigationBar.title = @"充值";
    
    UIButton *recordBtn = [UIButton buttonWithType:0];
    [recordBtn setTitle:@"充值记录" forState:0];
    [recordBtn setTitleColor:RGB(54, 124, 253) forState:0];
    recordBtn.titleLabel.font = FONT(12);
    [recordBtn sizeToFit];
    recordBtn.centerY = NAVBAR_CENTER_Y;
    recordBtn.right = FULL_WIDTH - 10;
    [recordBtn addTarget:self action:@selector(handleRecord) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationBar addSubview:recordBtn];
    
    self.view.backgroundColor = QTZ_Gray237;
    [self configView];
}

- (void)handleRecord
{
    [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController pushViewController:[XlpRCUJLCMineTopUpRecordControllerejjHxQ new] animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configView
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 13 + NAVBAR_HEIGHT, FULL_WIDTH, 100)];
    bgView.backgroundColor = QTZ_White;
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
    
    UILabel *amountTitleLabel = [UILabel labelWithText:@"充值金额" textColor:QTZ_Gray51 font:FONT(16)];
    amountTitleLabel.left = 11;
    amountTitleLabel.centerY = 25;
    [bgView addSubview:amountTitleLabel];
    
    UILabel *amountCountLabel = [UILabel labelWithText:@"元" textColor:QTZ_Gray51 font:FONT(16)];
    amountCountLabel.right = FULL_WIDTH - 11;
    amountCountLabel.centerY = amountTitleLabel.centerY;
    [bgView addSubview:amountCountLabel];
    
    self.rcTBzdamountTextFieldnUbSfR = [[UITextField alloc] initWithFrame:CGRectMake(amountTitleLabel.right + 30, 0, amountCountLabel.left - 10 - amountTitleLabel.right - 30, 20)];
    self.rcTBzdamountTextFieldnUbSfR.centerY = amountTitleLabel.centerY;
    self.rcTBzdamountTextFieldnUbSfR.textAlignment = NSTextAlignmentRight;
    self.rcTBzdamountTextFieldnUbSfR.placeholder = @"请输入充值金额";
    self.rcTBzdamountTextFieldnUbSfR.textColor = Theme_Color;
    self.rcTBzdamountTextFieldnUbSfR.font = FONT(16);
    self.rcTBzdamountTextFieldnUbSfR.keyboardType = UIKeyboardTypeNumberPad;
    self.rcTBzdamountTextFieldnUbSfR.delegate = self;
    [bgView addSubview:self.rcTBzdamountTextFieldnUbSfR];
    [amountTitleLabel.layer addSublayer:[CALayer addLineOriginPoint:CGPointMake(0, amountTitleLabel.height + 12) toPoint:CGPointMake(amountCountLabel.right, amountTitleLabel.height + 12) color:QTZ_Gray237 borderWidth:.5]];
    
    UILabel *bankTitleLabel = [UILabel labelWithText:@"银行卡号" textColor:QTZ_Gray51 font:FONT(16)];
    bankTitleLabel.left = 11;
    bankTitleLabel.centerY = 75;
    [bgView addSubview:bankTitleLabel];
    
    NSString *bankcardNum = [NSString new];
    if (self.nEvvdBfullCardDatakvLLBq) {
        bankcardNum = [NSString stringWithFormat:@"%@(尾号%@)", self.nEvvdBfullCardDatakvLLBq.bankCardNo, [self.nEvvdBfullCardDatakvLLBq.bankCardNo substringWithRange:NSMakeRange(self.nEvvdBfullCardDatakvLLBq.bankCardNo.length - 4, 4)]];
    }
    if (self.JdRIdxcardinfoDataQaLyXt) {
        bankcardNum = [NSString stringWithFormat:@"尾号%@", self.JdRIdxcardinfoDataQaLyXt.shortCard];
    }
    
    UILabel *bankLabel = [UILabel labelWithText:bankcardNum textColor:QTZ_Gray51 font:FONT(16)];
    bankLabel.right = FULL_WIDTH - 11;
    bankLabel.centerY = 75;
    [bgView addSubview:bankLabel];
//    [bankTitleLabel.layer addSublayer:[CALayer addLineOriginPoint:CGPointMake(0, bankLabel.height + 12) toPoint:CGPointMake(bankLabel.right, bankLabel.height + 12) color:QTZ_Gray237 borderWidth:.5]];
    
    
//    UILabel *msgLabel = [UILabel labelWithText:@"姓名" textColor:QTZ_Gray51 font:FONT(16)];
//    msgLabel.left = 11;
//    msgLabel.centerY = 125;
//    [bgView addSubview:msgLabel];
    
//    self.LufDjKnameLabelRjatWG = [[UITextField alloc] initWithFrame:CGRectMake(msgLabel.right + 30, 0, FULL_WIDTH - msgLabel.right - 30 - 11, 30)];
//    self.aSHWXpmsgTextFieldgHtMSl.centerY = msgLabel.centerY;
//    self.aSHWXpmsgTextFieldgHtMSl.textAlignment = NSTextAlignmentRight;
//    self.aSHWXpmsgTextFieldgHtMSl.font = FONT(15);
//    self.aSHWXpmsgTextFieldgHtMSl.delegate = self;
//    self.aSHWXpmsgTextFieldgHtMSl.placeholder = @"请输入您的验证码";
//    self.aSHWXpmsgTextFieldgHtMSl.keyboardType = UIKeyboardTypeNumberPad;
//    self.aSHWXpmsgTextFieldgHtMSl.returnKeyType = UIReturnKeyDone;
//    self.aSHWXpmsgTextFieldgHtMSl.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [bgView addSubview:self.aSHWXpmsgTextFieldgHtMSl];
    
//    self.msgBtn = [UIButton new];
//    self.msgBtn.titleLabel.font = FONT(9);
//    [self.msgBtn setTitleColor:Theme_Color forState:0];
//    [self.msgBtn setTitle:@"发送验证码" forState:0];
//    [self.msgBtn sizeToFit];
//    [self.msgBtn addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
//    self.aSHWXpmsgTextFieldgHtMSl.rightView = self.msgBtn;
//    self.aSHWXpmsgTextFieldgHtMSl.rightViewMode = UITextFieldViewModeAlways;
 
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(38, FULL_HEIGHT - 67, FULL_WIDTH - 76, 40)];
    [confirmBtn setBackgroundColor:Theme_Color];
    confirmBtn.titleLabel.font = FONT(15);
    [confirmBtn setTitle:@"确认充值" forState:0];
    [confirmBtn setTitleColor:QTZ_White forState:0];
    confirmBtn.layer.cornerRadius = 5;
    [confirmBtn addTarget:self action:@selector(handleConfirm) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
    
//    [self.rcTBzdamountTextFieldnUbSfR becomeFirstResponder];
}

- (void)handleConfirm
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.nEvvdBfullCardDatakvLLBq) {
        [params setSafeObject:self.rcTBzdamountTextFieldnUbSfR.text forKey:@"amt"];
//        [params setSafeObject:self.nEvvdBfullCardDatakvLLBq.realRealBankCardNo forKey:@"bankCard"];
        [params setSafeObject:self.nEvvdBfullCardDatakvLLBq.realName forKey:@"name"];
        [params setSafeObject:self.nEvvdBfullCardDatakvLLBq.realPid forKey:@"pid"];
        [params setSafeObject:@"app" forKey:@"payChannel"];
        [params setSafeObject:CHANNEL forKey:@"channel"];
        [params setSafeObject:self.nEvvdBfullCardDatakvLLBq.realBankCardNo forKey:@"bankCard"];
    }else{
        [params setSafeObject:self.rcTBzdamountTextFieldnUbSfR.text forKey:@"amt"];
        [params setSafeObject:self.name forKey:@"name"];
        [params setSafeObject:self.pid forKey:@"pid"];
        [params setSafeObject:@"app" forKey:@"payChannel"];
        [params setSafeObject:CHANNEL forKey:@"channel"];
        [params setSafeObject:self.JdRIdxcardinfoDataQaLyXt.realBankCardNo forKey:@"bankCard"];
    }
    
    
    
    [sHkPtEJLCFMineRequestozAYZs topUpWith:params loadingInView:self.view success:^(YgMWMCJLCFFuyouInfoModelNQcnsr *data){
        [[NSNotificationCenter defaultCenter] postNotificationName:MINE_REFRESH object:nil];
        JLCFBaseWebViewController *webvc = [[JLCFBaseWebViewController alloc]init];
        webvc.url = data.url;
        webvc.fuyouinfo = data;
        [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webvc animated:YES];
//        [self showNotice:nil image:[UIImage imageNamed:@"icon_topUp_success"] afterDelay:1.5f];
        if (self.RechargeComplete) {
            self.RechargeComplete();
        }
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [[JLCFApplicationEntrace shareEntrance].currentNavController popViewControllerAnimated:YES];
//        });
    } failure:^(NSString *error) {
        [self showNotice:error];
//        [self showNotice:error image:[UIImage imageNamed:@"icon_topUp_failure"] afterDelay:1.5];
    }];
    
}


//获取验证码
- (void)getVerifyCode
{
    [sHkPtEJLCFMineRequestozAYZs codeBankWithParams:@{@"type" : @"6"} loadingInView:nil success:^{
        [self handleVerifyBtn];
        [self showNotice:@"验证码发送成功"];
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
    
}

- (void)handleVerifyBtn
{
    _count = showtime;
    [[NSRunLoop mainRunLoop] addTimer:self.qdZZsdcountTimereVpqYS forMode:NSRunLoopCommonModes];
}

- (NSTimer *)qdZZsdcountTimereVpqYS
{
    if (!_qdZZsdcountTimereVpqYS) {
        _qdZZsdcountTimereVpqYS = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    }
    return _qdZZsdcountTimereVpqYS;
}

- (void)countDown
{
    _count --;
    [self.msgBtn setTitle:[NSString stringWithFormat:@"倒计时%ds",_count] forState:UIControlStateNormal];
    [self.msgBtn setTitleColor:QTZ_Gray173 forState:0];
    self.msgBtn.userInteractionEnabled = NO;
    if (_count == 0) {
        [self.msgBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.msgBtn setTitleColor:Theme_Color forState:0];
        self.msgBtn.userInteractionEnabled = YES;
        [self dismiss];
    }
    [self.msgBtn sizeToFit];
}

- (void)dismiss
{
    [self.qdZZsdcountTimereVpqYS invalidate];
    self.qdZZsdcountTimereVpqYS = nil;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

@end
